﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using log4net;

namespace ClimateControl.Infrastructure.Service
{
    public class ClimateClient : DuplexClientBase<IClimateClient>, IClimateClient
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ClimateClient));

        #region Constructor

        public ClimateClient(InstanceContext callback, EventHandler exceptionHandler, string location)
            : base(callback, ServiceHelper.GetBinding(), ServiceHelper.GetEndpointAddress(location))
        {
            this.ChannelFactory.Faulted += exceptionHandler;
            this.InnerChannel.Faulted += exceptionHandler;
            this.InnerDuplexChannel.Faulted += exceptionHandler;

            Subscribe();
        }

        #endregion Constructor

        #region Service Methods

        public void Subscribe()
        {
            try
            {
                this.Channel.Subscribe();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }   
        }

        public void Vote(VoteOption vote, string user)
        {
            try
            {
                Channel.Vote(vote, user);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }
        }

        public ClimateInfo GetClimateInfo()
        {
            return this.Channel.GetClimateInfo();
        }

        #endregion Service Methods
    }
}
